<template>
  <div class="container">
    <div class="top">
      <div class="name">
        <div>
          <span>{{ titleName }}</span>
        </div>
      </div>
      <div class="button">
        <template v-for="(item, index) in buttonArr">
          <el-tooltip
            class="item"
            effect="dark"
            :content="item.content"
            :disabled="!item.disabled"
            :placement="item.placement || 'top'"
            :key="index"
            v-if="!item.hide"
          >
            <div
              style="display: inline-block; margin: 0 5px"
              v-if="!item.isHide"
            >
              <el-button
                type="plain"
                class="btn-text-blue"
                :key="index"
                :disabled="item.disabled"
                size="mini"
                :icon="item.icon"
                @click="item.method"
                >{{ item.name }}</el-button
              >
            </div>
          </el-tooltip>
        </template>
      </div>
    </div>
    <div class="first">
      <dividerTitle :title="'分类信息'"></dividerTitle>
      <div class="edit-form">
        <editForm
          :formConfig="formConfig"
          ref="editForm"
          :formData="editFormData"
        ></editForm>
      </div>
    </div>
    <div class="middle" v-if="isVirtual != '1'">
      <dividerTitle :title="'基本参数'"></dividerTitle>
      <div class="content">
        <div class="select">
          <el-row>
            <el-col :span="8">
              <label class="item-label">基本参数组</label>
              <el-select
                class="input"
                size="mini"
                v-model="groupId"
                placeholder="请选择"
                @change="selectBaseParam"
              >
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-col>
          </el-row>
        </div>
        <div class="param">
          <label class="desc">预设参数：</label>
          <div class="list">
            <span
              class="item"
              v-for="(item, index) in presetParamList"
              :key="index"
            >
              {{ item }}
            </span>
          </div>
        </div>
        <div class="param">
          <label class="desc">自定义参数：</label>
          <div class="list">
            <span
              class="item base-item"
              v-for="(item, index) in baseParam"
              :key="index"
            >
              {{ item }}
            </span>
          </div>
        </div>
      </div>
    </div>
    <div class="buttom" v-if="isVirtual != '1'">
      <dividerTitle :title="'技术参数'"></dividerTitle>
      <customParams :baseParamList="techParamList" />
    </div>
    <dialogPublic
      :title="'导入文件'"
      :show.sync="show"
      width="40%"
      :noFootBtn="true"
    >
      <div style="text-align: left; height: 200px" v-if="show">
        <el-upload
          class="upload-demo"
          :action="uploadUrl"
          :on-change="handleChange"
          :multiple="false"
          :auto-upload="false"
          accept=".xls,.xlsx"
        >
          <el-button slot="trigger" size="small" type="primary"
            >选取文件</el-button
          >
          <el-button
            style="margin-left: 10px"
            size="small"
            type="success"
            @click="submitUpload"
            >上传到服务器</el-button
          >
          <div class="el-upload__tip" slot="tip">
            只能上传excle文件，且不超过500kb
          </div>
        </el-upload>
        <el-button v-if="path != ''" type="text" @click="downLoadResult"
          >下载错误备注文件</el-button
        >
      </div>
    </dialogPublic>
  </div>
</template>

<script>
import {
  classStandardDetail,
  classStandardAddOrEdit,
  classStandardDelete,
  baseParamDetail,
  baseParamList,
  importDeviceClassify,
} from "@/api/standardManagement/acountManagement.js";
import customParams from "../baseParameter/customParams.vue";
export default {
  props: ["treeNodeId", "treeNodeName"],
  components: {
    customParams,
  },
  data() {
    return {
      title: "",
      show: false,
      path: "",
      uploadUrl: `${process.env.VUE_APP_BASEURL}/eam-service-standard/deviceClassify/import`,
      titleName: "编辑节点",
      isVirtual: 1,
      value: "text",
      isMultiSelect: false,
      groupId: "",
      buttonArr: [
        {
          name: "根节点",
          icon: "icon-genjiedian",
          isHide: false,
          disabled: !this.$store.getters.isBtnAuth("addRootNode"), //控制按钮的禁用
          content: "未授权功能", //禁用提示内容
          method: () => {
            this.clearData();
            this.titleName = "添加根节点";
          },
        },
        {
          name: "节点",
          icon: "icon-jiedian",
          isHide: false,
          disabled: !this.$store.getters.isBtnAuth("addNode"), //控制按钮的禁用
          content: "未授权功能", //禁用提示内容
          method: () => {
            this.clearData();
            this.titleName = "添加节点";
          },
        },
        {
          name: "保存",
          icon: "icon-a-wanchengbaocun",
          isHide: false,
          disabled: !this.$store.getters.isBtnAuth("save"), //控制按钮的禁用
          content: "未授权功能", //禁用提示内容
          method: () => {
            this.save();
          },
        },
        {
          name: "删除",
          icon: "icon-shanchu",
          isHide: false,
          disabled: !this.$store.getters.isBtnAuth("delete"), //控制按钮的禁用
          content: "未授权功能", //禁用提示内容
          method: () => {
            this.deleteData();
          },
        },
        {
          name: "拖拽节点",
          icon: "icon-yidongjiedian",
          isHide: false,
          disabled: !this.$store.getters.isBtnAuth("moveNode"), //控制按钮的禁用
          content: "未授权功能", //禁用提示内容
          method: () => {
            this.moveNode();
          },
        },
        {
          name: "取消拖拽",
          icon: "icon-quxiaotuozhuai",
          isHide: true,
          disabled: !this.$store.getters.isBtnAuth("moveNode"), //控制按钮的禁用
          content: "未授权功能", //禁用提示内容
          method: () => {
            this.caleMoveNode();
          },
        },
        {
          name: "导入",
          icon: "icon-daoru",
          disabled: !this.$store.getters.isBtnAuth("import"), //控制按钮的禁用
          content: "未授权功能", //禁用提示内容
          method: () => {
            this.import();
          },
        },
        {
          name: "下载模板",
          icon: "icon-xiazai",
          disabled: !this.$store.getters.isBtnAuth("download"), //控制按钮的禁用
          content: "未授权功能", //禁用提示内容
          method: () => {
            this.download();
          },
        },
      ],
      options: [],
      presetParamList: [],
      baseParam: [],
      //技术参数
      //弹出框表单配置信息
      formConfig: {
        labelWidth: "100px",
        itemArr: [
          {
            label: "分类名称",
            prop: "name",
            type: "text",
            span: 24,
            maxlength: 128,
            required: true,
            methods: (val) => {
              this.editFormData.name = val;
            },
          },
          {
            label: "分类编码",
            prop: "code",
            type: "text",
            span: 8,
            disabled: true,
          },
          {
            label: "分类类型",
            prop: "isVirtual",
            type: "radio",
            span: 8,
            radioOption: this.radioOption,
            required: true,
            methods: (val) => {
              this.editFormData.isVirtual = val.value;
              this.isVirtual = val.value;
            },
          },
          {
            label: "专业",
            prop: "major",
            type: "select",
            span: 8,
            isHide: false,
            required: true,
            selectOption: this.$store.getters.dirtData("common_major"),
          },
          {
            label: "ID",
            prop: "id",
            type: "text",
            span: 8,
            disabled: true,
          },
          {
            label: "层级编号",
            prop: "path",
            type: "text",
            span: 8,
            disabled: true,
          },
          
        ],
      },
      //弹出框表单数据
      editFormData: {
        isVirtual: 0,
      },
      presetList: [],
      //技术参数
      techParamList: [],
    };
  },
  watch: {
    treeNodeId: {
      handler(val) {
        if (val != "") {
          this.titleName = "编辑节点";
          this.getParamTable(val);
        }
      },
    },
    isVirtual: {
      handler(val) {
        this.controlMajor(val);
      },
    },
  },
  computed: {
    radioOption: function () {
      return this.$store.getters
        .dirtData("standard_device_type")
        .map((item) => {
          let obj = {};
          obj.label = item.label;
          obj.value = parseInt(item.value);
          return obj;
        });
    },
  },
  created() {
    this.$set(this.formConfig.itemArr[2], "radioOption", this.radioOption);
    this.getBaseParam();
  },
  methods: {
    //控制专业下拉框是否展示
    controlMajor(val) {
      if (val == 1) {
        this.formConfig.itemArr[3].isHide = true;
        this.groupId = "";
        this.presetParamList.length = 0;
        this.baseParam.length = 0;
        this.techParamList.length = 0;
      } else {
        this.formConfig.itemArr[3].isHide = false;
      }
    },
    //下拉选择基本参数
    selectBaseParam(value) {
      if (value != "") {
        this.getBaseParamdetail(value);
      } else {
        this.presetParamList.length = 0;
        this.baseParam.length = 0;
      }
    },
    //拖拽节点
    moveNode() {
      this.buttonArr[4].isHide = true;
      this.buttonArr[5].isHide = false;
      this.$emit("moveNode", true);
    },
    //取消拖拽
    caleMoveNode() {
      this.buttonArr[4].isHide = false;
      this.buttonArr[5].isHide = true;
      this.$emit("caleMoveNode", false);
    },
    //保存按钮
    async save() {
      let data = await this.$refs.editForm.getFormData();
      const params = Object.assign({}, data);
      params.groupId = this.groupId;
      params.typeParamsList = this.techParamList;
      if (this.isVirtual != 1 && this.groupId == "") {
        this.$message.warning("基本参数组不能为空");
        return;
      }
      if (this.titleName == "编辑节点") {
        delete params.typeGroupParamsList;
        classStandardAddOrEdit(params).then(
          (res) => {
            if (res.code === 200) {
              this.$message.success(res.description);
              this.$emit("save");
            }
          },
          (error) => {
            this.$message.error(error);
          }
        );
      } else {
        params.parentId = this.titleName == "添加节点" ? this.treeNodeId : "0";
        params.parentName =
          this.titleName == "添加节点" ? this.treeNodeName : "0";
        params.typeParamsList = this.techParamList;
        classStandardAddOrEdit(params).then((res) => {
          if (res.code === 200) {
            this.$message.success(res.description);
            this.titleName = "编辑节点";
            this.$emit("add", res.data);
          }
        });
      }
    },
    //删除分类标准
    deleteData() {
      this.$confirm("确定要删除这条数据吗", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          classStandardDelete({ id: this.treeNodeId }).then(
            (res) => {
              if (res.code == 200) {
                this.$message.success(res.description);
                this.$emit("delete");
              }
            },
            (error) => {
              this.$message.error(error);
            }
          );
        })
        .catch(() => {});
    },
    //获取基本参数下拉列表
    getBaseParam() {
      baseParamList()
        .then((res) => {
          if (res.code == 200) {
            this.options = res.data.map((item) => {
              let obj = {};
              obj.label = item.name;
              obj.value = item.id;
              return obj;
            });
          }
        })
        .catch((res) => {
          this.$message.error(res.description);
          return;
        });
    },
    //获取基本参数详情
    getBaseParamdetail(value) {
      const params = {
        id: value,
      };
      baseParamDetail(params).then(
        (res) => {
          if (res.code == 200) {
            this.presetParamList =
              res.data.typeGroupDefaultParamsList.length != 0
                ? res.data.typeGroupDefaultParamsList.map((item) => {
                    return item.paramName;
                  })
                : [];
            this.baseParam =
              res.data.typeGroupCustomParamsList.length != 0
                ? res.data.typeGroupCustomParamsList.map((item) => {
                    return item.paramName;
                  })
                : [];
          }
        },
        (error) => {
          this.$message.error(error);
        }
      );
    },
    //获取分类标准详情
    getParamTable(id) {
      classStandardDetail({ id }).then(
        (res) => {
          if (res.code == 200) {
            this.editFormData = res.data;
            this.isVirtual = this.editFormData.isVirtual;
            this.editFormData.path = this.editFormData.levelPath.replace(/\//g,".")
            this.groupId = res.data.groupId;
            if (this.groupId != "" && this.groupId != null) {
              this.getBaseParamdetail(this.groupId);
            }
            this.techParamList = res.data.typeParamsList;
          }
        },
        (error) => {
          this.$message.error(error);
        }
      );
    },
    //导入
    import() {
      this.show = true;
    },
    //文件上传之前
    handleChange(file) {
      let formData = new FormData();
      formData.append("file", file.raw);
      this.uploadFile = formData;
    },
    submitUpload() {
      importDeviceClassify(this.uploadFile).then((res) => {
        if (typeof res.data == "string") {
          this.path = `eam-service-standard/${res.data}`;
          this.$message.error("导入失败，请下载批复文件查看");
        } else {
          this.$message.success("导入成功");
          this.show = false;
          this.$emit("save");
        }
      });
    },
    //点击下载批复文件
    downLoadResult() {
      this.$utils.downloadFile(null, "分类标准批复", this.path);
    },
    //下载模板
    download() {
      let path = "eam-service-standard/deviceClassify/template";
      let fileName = "分类标准导入模板";
      this.$utils.downloadFile(null, fileName, path);
    },
    clearData() {
      this.editFormData = {};
      this.isVirtual = 1;
      this.$set(this.editFormData, "isVirtual", 1);
      this.presetParamList = [];
      this.baseParam = [];
      this.techParamList = [];
    },
  },
};
</script>

<style lang="less" scoped>
.container {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  .top {
    height: 50px;
    display: flex;
    justify-content: space-between;
    .name {
      font-size: 16px;
      font-weight: bold;
      padding: 0 10px;
      letter-spacing: 4px;
      height: 40px;
      line-height: 40px;
      text-align: left;
      padding: 0 10px;
    }
    .button {
      height: 40px;
      line-height: 40px;
    }
  }
  .first {
    height: 240px;
    .edit-form {
      margin: 10px 0;
    }
  }
  .middle {
    flex: 3;
    .content {
      margin: 20px 0;
      text-align: left;
      font-size: 14px;
      .select {
        margin: 10px 0 20px 26px;
        .item-label::before {
          content: "*";
          color: red;
          margin-right: 4px;
        }
        .input {
          width: calc(100% - 102px);
          margin-left: 20px;
        }
      }
      .param {
        margin: 10px 10px;
        display: flex;
        .desc {
          text-align: right;
          width: 100px;
          height: 40px;
          line-height: 40px;
        }
        .item {
          margin: 5px 10px;
          text-align: center;
          height: 40px;
          line-height: 40px;
          padding: 0 10px;
          background-color: #f2f2f2;
          border-radius: 10px;
        }
        .base-item {
          background-color: #d9f2fd;
        }
        .list {
          flex: 1;
          display: flex;
          flex-wrap: wrap;
        }
      }
    }
  }
  .buttom {
    flex: 4;
    .add {
      text-align: right;
      margin: 10px 0;
    }
  }
}
.title {
  width: 100%;
  font-size: 16px;
  font-weight: bold;
  padding: 0 10px;
  letter-spacing: 4px;
  height: 40px;
  line-height: 40px;
  text-align: left;
  background-color: #e8f1f8;
}
</style>
